<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="" prop="businessId">
        <el-input
          style="width: 140px"
          v-model="queryParams.businessId"
          placeholder="请输入商户订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="wxPayId">
        <el-input
          style="width: 140px"
          v-model="queryParams.wxPayId"
          placeholder="请输入微信订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="consumerAccountId">
        <el-input
          style="width: 140px"
          v-model="queryParams.consumerAccountId"
          placeholder="请输入用户账户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="openid">
        <el-input
          style="width: 140px"
          v-model="queryParams.openid"
          placeholder="请输入openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="payType">
        <el-select v-model="queryParams.payType" class="labelWidth" clearable style="width: 150px"
                   placeholder="当前订单类型">
          <el-option
            key="0"
            label="充电订单"
            value="0">
          </el-option>
          <el-option
            key="1"
            label="余额充值"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="status">
        <el-select
          v-model="queryParams.status"
          style="width: 150px"
          placeholder="请选择支付状态"
          clearable
          size="small">
          <el-option
            v-for="(val, key, index) in statusList"
            :key="key"
            :label="val"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="profitSharing">
        <el-select
          v-model="queryParams.profitSharing"
          style="width: 150px"
          placeholder="请选择分账标记"
          clearable
          size="small">
          <el-option
            v-for="(val, key, index) in profitSharing"
            :key="key"
            :label="val"
            :value="key"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="timeExport">
        <el-date-picker
          v-model="queryParams.timeExport"
          :default-time="['00:00:00', '23:59:59']"
          align="right"
          clearable
          end-placeholder="下单结束日期"
          range-separator="至"
          start-placeholder="下单开始日期"
          style="width: 270px"
          type="daterange"
          value-format="timestamp"
          @change="timeChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['business:payrank:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['business:payrank:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['business:payrank:remove']"
              >删除</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:payrank:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payrankList" @selection-change="handleSelectionChange" :stripe="true">
      <el-table-column
        align="center"
        label="支付订单id"
        prop="payId"/>
      <el-table-column
        align="center"
        label="商户订单号"
        prop="businessId"/>
      <el-table-column
        align="center"
        label="微信订单号"
        prop="wxPayId"/>
      <el-table-column
        align="center"
        label="用户账户ID"
        prop="consumerAccountId"/>
      <el-table-column
        align="center"
        label="openid"
        prop="openid"/>
      <el-table-column
        align="center"
        label="订单类型"
        prop="payType">
        <template v-slot="scope">
          {{ scope.row.payType == '0' ? '充电订单' : '余额充值' }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="支付金额"
        prop="payMoneyTotal">
        <template v-slot="scope">
          {{ scope.row.payMoneyTotal / 100 }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="支付状态"
        prop="status">
        <template slot-scope="scope">
          {{ findStatus(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="下单时间"
        prop="creatTime">
        <template v-slot="scope">
          {{ parseTime(scope.row.creatTime) }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="退款金额"
        prop="refundMoney">
        <template v-slot="scope">
          {{ scope.row.refundMoney / 100 }}
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="分账标记"
        prop="profitSharing">
        <template slot-scope="scope">
          {{ profitSharing[scope.row.profitSharing] }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['business:payrank:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['business:payrank:remove']"
                >删除</el-button>
              </template>
            </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="业务id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入业务id" />
        </el-form-item>
        <el-form-item label="消费者账户ID" prop="consumerAccountId">
          <el-input v-model="form.consumerAccountId" placeholder="请输入消费者账户ID" />
        </el-form-item>
        <el-form-item label="创建时间" prop="creatTime">
          <el-input v-model="form.creatTime" placeholder="请输入创建时间" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastUpdateTime">
          <el-input v-model="form.lastUpdateTime" placeholder="请输入最后修改时间" />
        </el-form-item>
        <el-form-item label="微信用户id" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入微信用户id" />
        </el-form-item>
        <el-form-item label="退款单号" prop="outRefundNo">
          <el-input v-model="form.outRefundNo" placeholder="请输入退款单号" />
        </el-form-item>
        <el-form-item label="总金额(以分为单位)" prop="payMoneyTotal">
          <el-input v-model="form.payMoneyTotal" placeholder="请输入总金额(以分为单位)" />
        </el-form-item>
        <el-form-item label="退款金额" prop="refundMoney">
          <el-input v-model="form.refundMoney" placeholder="请输入退款金额" />
        </el-form-item>
<!--        <el-form-item label="数据版本" prop="version">-->
<!--          <el-input v-model="form.version" placeholder="请输入数据版本" />-->
<!--        </el-form-item>-->
        <el-form-item label="微信支付订单id" prop="wxPayId">
          <el-input v-model="form.wxPayId" placeholder="请输入微信支付订单id" />
        </el-form-item>
        <el-form-item label="赠送金额" prop="presentedMoney">
          <el-input v-model="form.presentedMoney" placeholder="请输入赠送金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayrank, getPayrank, delPayrank, addPayrank, updatePayrank } from "@/api/business/payrank";

const statusList = {
  0: '创建',
  1: '支付成功',
  2: '未支付',
  3: '已关闭',
  4: '转入退款',
  5: '退款成功',
  6: '退款关闭',
  7: '退款异常',
  8: '退款处理中'
}

const profitSharing = {
  0: '不可分账', 1: '可以分账', 2: '已分账'
}
export default {
  name: "Payrank",
  data() {
    return {
      statusList,
      profitSharing,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值记录表格数据
      payrankList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payId: null,
        businessId: null,
        consumerAccountId: null,
        creatTime: null,
        lastUpdateTime: null,
        openid: null,
        outRefundNo: null,
        payMoneyTotal: null,
        refundMoney: null,
        status: null,
        version: null,
        wxPayId: null,
        payType: null,
        presentedMoney: null,
        advancePayment: null,
        wxRefund: null,
        wxPayResults: null,
        wxPayRefundResults: null,
        beginTime: null,
        endTime: null,
        timeExport: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        businessId: [
          { required: true, message: "业务id(和微信平台交互的业务订单id)不能为空", trigger: "blur" }
        ],
        consumerAccountId: [
          { required: true, message: "消费者账户ID不能为空", trigger: "blur" }
        ],
        creatTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        openid: [
          { required: true, message: "微信用户id不能为空", trigger: "blur" }
        ],
        payMoneyTotal: [
          { required: true, message: "总金额(以分为单位)不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "支付状态(0:创建  1:支付成功  2:未支付  3:已关闭 4:转入退款 5:退款成功 6: 退款关闭 7:退款异常 8:退款处理中 )不能为空", trigger: "change" }
        ],
        payType: [
          { required: true, message: "充值订单类型 (0 充值订单 1 余额充值)不能为空", trigger: "change" }
        ],
        presentedMoney: [
          { required: true, message: "赠送金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    timeChange(val) {
      if (!val) {
        this.queryParams.beginTime = null
        this.queryParams.endTime = null
        return
      }
      this.queryParams.beginTime = val[0]
      this.queryParams.endTime = val[1]
    },
    findStatus(key) {
      return statusList[key]
    },
    /** 查询充值记录列表 */
    getList() {
      this.loading = true;
      listPayrank(this.queryParams).then(response => {
        this.payrankList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payId: null,
        businessId: null,
        consumerAccountId: null,
        creatTime: null,
        lastUpdateTime: null,
        openid: null,
        outRefundNo: null,
        payMoneyTotal: null,
        refundMoney: null,
        status: null,
        version: null,
        wxPayId: null,
        payType: null,
        presentedMoney: null,
        advancePayment: null,
        wxRefund: null,
        wxPayResults: null,
        wxPayRefundResults: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.beginTime = null
      this.queryParams.endTime = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.payId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const payId = row.payId || this.ids
      getPayrank(payId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.payId != null) {
            updatePayrank(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayrank(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const payIds = row.payId || this.ids;
      this.$modal.confirm('是否确认删除充值记录编号为"' + payIds + '"的数据项？').then(function() {
        return delPayrank(payIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/payrank/export', {
        ...this.queryParams
      }, `payrank_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
